<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="合同名称" prop="contarctName">
        <el-input
          v-model="queryParams.contarctName"
          placeholder="请输入合同名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contarctCode">
        <el-input
          v-model="queryParams.contarctCode"
          placeholder="请输入合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="contactMobile">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="contarctList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="合同编号" align="center" prop="contarctCode" />
      <el-table-column label="合同名称" align="center" prop="contarctName" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="通讯地址" align="center" prop="contactAddress" />
      <el-table-column label="联系人名称" align="center" prop="contactName" />
      <el-table-column label="联系人电话" align="center" prop="contactMobile" />
      <el-table-column label="合同类型" align="center" prop="contarctName" />
      <el-table-column label="合同状态" align="center" prop="contarctStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contarct_status" :value="scope.row.contarctStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="物管合同号" align="center" prop="propertyCode" />
      <el-table-column label="签订日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁开始期限" align="center" prop="contractStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁结束期限" align="center" prop="contractEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁方" align="center" prop="contactName" />
      <el-table-column label="客户名称" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactMobile" />
      <el-table-column width="140" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >作废</el-button>
          <el-button
            v-show="scope.row.contarctStatus === 2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBill(scope.row)"
            v-hasPermi="['contarct:contarct:edit']"
          >生成账单
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSurrender(scope.row)"
            v-hasPermi="['contarct:contarct:remove']"
          >退租</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同对话框 -->
    <el-dialog title="作废" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否作废" prop="contarctStatus">
          <el-radio v-model="form.contarctStatus" :label="5">是</el-radio>
          <el-radio v-model="form.contarctStatus" :label="!5">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContarct, contarctToVoid, delContarct } from "@/api/contarct/contarct";

export default {
  name: "Contarct",
  dicts: ['sys_status', 'contarct_status', 'rent_month_type', 'unit_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同表格数据
      contarctList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contarctCode: null,
        rentCode: null,
        propertyCode: null,
        contarctName: null,
        companyId: null,
        companyName: null,
        contactAddress: null,
        contactName: null,
        contactMobile: null,
        contractStartTime: null,
        contractEndTime: null,
        createTime: null,
        freeStarttime: null,
        freeEndTime: null,
        roomids: null,
        status: null,
        contarctPath: null,
        updateTime: null,
        signBy: null,
        signTime: null,
        bondfee: null,
        bondmouth: null,
        allarea: null,
        contarctStatus: 2,
        contactEmail: null,
        rentMonthType: null,
        rentfee: null,
        rentfeeUnit: null,
        rentfeeDay: null,
        rentfeeMonth: null,
        propertyfee: null,
        propertyfeeUnit: null,
        properDay: null,
        properMonth: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  activated() {
    this.getList();
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true;
      listContarct(this.queryParams).then(response => {
        this.contarctList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contarctCode: null,
        rentCode: null,
        propertyCode: null,
        contarctName: null,
        companyId: null,
        companyName: null,
        contactAddress: null,
        contactName: null,
        contactMobile: null,
        contractStartTime: null,
        contractEndTime: null,
        createTime: null,
        freeStarttime: null,
        freeEndTime: null,
        roomids: null,
        status: null,
        contarctPath: null,
        updateTime: null,
        signBy: null,
        signTime: null,
        createBy: null,
        updateBy: null,
        bondfee: null,
        bondmouth: null,
        allarea: null,
        contarctStatus: null,
        contactEmail: null,
        rentMonthType: null,
        rentfee: null,
        rentfeeUnit: null,
        rentfeeDay: null,
        rentfeeMonth: null,
        propertyfee: null,
        propertyfeeUnit: null,
        properDay: null,
        properMonth: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同";
    },
    /** 作废按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.conductionData.contractId = row.id || this.ids
      this.form.contarctStatus = 5;//默认选中“是”
      this.open = true;
      this.title = "修改合同";

    },
    /** 提交按钮 */
    submitForm() {
      var status = this.form.contarctStatus;
      if(status === 5) {
        contarctToVoid(this.conductionData.contractId).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      }else{
        this.open = false;
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除合同编号为"' + ids + '"的数据项？').then(function() {
        return delContarct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('contarct/contarct/export', {
        ...this.queryParams
      }, `contarct_${new Date().getTime()}.xlsx`)
    },
    // 退租
    handleSurrender(row){
      this.conductionData.contractId = row.id
      this.$router.push({
        path:"/contarct/ContractSurrender"
      })
    },
    //合同详情
    handleDetail(row) {
      this.conductionData.contractId = row.id || this.ids[0]
      this.$router.push({
        path: "/contarct/ContarctDetails"
      });
    },
    // 生成账单
    handleBill(row) {
      this.conductionData.contractId = row.id
      this.$router.push({
        path: "/contarct/billindex"
      });
    },
  }
};
</script>
